// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      id:0,
      network: getApp().globalData["network"],
      detail:"",
      imgsUrl:[]
  },
  requestOne:function(id){
    var that =this;
    wx.request({
      url:"http://"+that.data.network+"/front/requestOne.php",
      method:"POST",
      data:{
        "id":id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success:function(data){
        console.log(data);
        that.setData({
          detail:data.data[0],
          imgsUrl:data.data[1]
        })
      }

    });
  },
  handleContact:function(e){
    console.log(e.detail.path)
      console.log(e.detail.query)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    var that = this;
    const eventChannel = this.getOpenerEventChannel();
    // eventChannel.emit('acceptDataFromOpenedPage', 99);
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function(data) {
        that.setData({
          id:data
        });
        console.log("data="+data);
        console.log(that.data.id);
        that.requestOne(that.data.id);
    });
    
    
  },
  tocart:function(){
    wx.reLaunch({
      url: '/pages/shoppingcart/shoppingcart',
    });
  },
  intocart:function(e){
    var that = this;
    var user_id = wx.getStorageSync("id");
    wx.request({
      url:"http://" + that.data.network +"/front/intocart.php",
      method: "post",
      datatype: "json",
      data:{
        user_id:user_id,
        goods_id:that.data.detail.id
      },
      header:{
        "Content-Type":"application/x-www-form-urlencoded"
      },
      success: function (data) {
        console.log(data);
        wx.showToast({
          title:"加入购物车",
          icon:"success",
          duration:1000
        });
      }
    });

  },
  tobuy: function (event) {
    var that = this;
    var user_id = wx.getStorageSync("id");
    let ordermoney = that.data.detail.goods_price;
    console.log("ordermoney=" + ordermoney);
    // 创建订单
    wx.request({
      url: "http://" + that.data.network + "/front/tobuy.php",
      method: "post",
      data: {
        user_id: user_id,
        ordermoney:ordermoney
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        console.log(data);
        wx.showToast({
          title: "创建成功",
          icon: "none",
          duration: 1000
        });
      }
    });
    // user_id=wx.getStorageSync("id");,goods_id=that.data.detail.id
    let goods_id = that.data.detail.id;
    wx.request({
      url:"http://"+that.data.network+"/front/subCart.php",
      method:"post",
      data:{
        user_id:user_id,
        goods_id:goods_id
      },
      header:{
        "Content-Type":"application/x-www-form-urlencoded"
      },
      success: function (data) {
        console.log(data);
        wx.navigateTo({
          url:"/pages/order/order", 
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})